package com.ic.loan.activity

import android.content.Intent
import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.StringUtils
import com.gyf.immersionbar.ktx.immersionBar
import com.ic.loan.R
import com.ic.loan.cache.Constant
import com.ic.loan.http.ApiUrl
import com.ic.loan.http.HttpUtils
import com.ic.loan.http.ResultListener
import com.ic.loan.pop.PrivacyPolicyPop
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_settings.*

class SettingsActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarDarkFont(true)
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        ivBack.setOnClickListener {
            onBackPressed()
        }
        llTerms.setOnClickListener {
            ActivityUtils.startActivity(TermsOfConditionsActivity::class.java)
        }
        llPrivacy.setOnClickListener {
                XPopup.Builder(this).asCustom(PrivacyPolicyPop(this)).show()
        }
        llContact.setOnClickListener {
            ActivityUtils.startActivity(ContactUsActivity::class.java)
        }
        tvLogout.setOnClickListener {
            XPopup.Builder(this).asConfirm(
                StringUtils.getString(R.string.tips),
                StringUtils.getString(R.string.tips_content),
                StringUtils.getString(R.string.cancel),
                StringUtils.getString(R.string.yes), {
                    logout()
                }, {}, false
            ).show()

        }
    }

    private fun logout() {
        showLoading()
        HttpUtils.postOkGo(
            mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),
            ApiUrl.POST_LOGOUT,
            object : ResultListener() {
                override fun onSucceeded(json: String?) {
                    SPUtils.getInstance().remove(Constant.TOKEN)
                    SPUtils.getInstance().remove(Constant.APPLY_SUCCESS)
                    ActivityUtils.startActivity(LoginActivity::class.java)
                }

                override fun onLoadFinish() {
                    super.onLoadFinish()
                    hintLoading()
                }
            })
    }
}